
from PyQt4.QtCore import QObject, SIGNAL

try:
    from numpy import ndarray
except ImportError:
    ndarray = None

import datetime 

class SCListDict(QObject):
    ''''''
    
    ACCEPTED_TYPES = (int,float,basestring,list,tuple,dict,ndarray,\
                      datetime.date,datetime.datetime,datetime.timedelta)
    
    def __init__(self,ws={},parent=None):
        self.__ws = ws
        QObject.__init__(self,parent)

    def __len__(self):
        return len(self.__ws)

    def __getitem__(self,key):
        return self.__ws[key]
    
    def __delitem__(self,key):
        del self.__ws[key]
        self.emit(SIGNAL("dictChanged()"))

    def __setitem__(self,key,val):
        assert self.__checkType__(val), "Wrong data type."
        self.__ws[key] = val
        self.emit(SIGNAL("dictChanged()"))
    
    def __checkType__(self,value):
        if isinstance(value,SCListDict.ACCEPTED_TYPES):
            return True
        return False

    def has_key(self,key):
        return self.__ws.has_key(key)
    
    def keys(self):
        return self.__ws.keys()
    
    def setData(self,ws):
        map(self.__checkType__,ws.values())
        self.__ws = ws
        self.emit(SIGNAL("dictChanged()"))

    def getData(self):
        return self.__ws
    
    def __indexToKey__(self,index):
        keys = sorted(self.__ws.keys())
        return keys[index]
        
    def valueFromIndex(self,index):
        key = self.__indexToKey__(index)
        return self.__ws[key]
    
    def valueToIndex(self,index,value):
        key = self.__indexToKey__(index)
        self.__ws[key] = value

    def keyFromIndex(self,index):
        return self.__indexToKey__(index)

        